﻿Imports System.IO
Imports System.Text
Imports Helper
Imports System.Windows.Forms
Imports Model.Geometry
Imports Model.Result
Imports Model
Imports System.Runtime.InteropServices

Public Module DataAccess
    ''' <summary>
    ''' 导入点云文件
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function importdata() As String
        Dim openDlg = New OpenFileDialog()
        openDlg.Filter = "点云文件|*.txt"
        openDlg.InitialDirectory = GlobalShared.defaultpath
        If openDlg.ShowDialog() = DialogResult.OK Then
            Return openDlg.FileName
        Else
            Return Nothing
        End If
    End Function
    ''' <summary>
    ''' 导入自定义里程列表
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function importMil() As String
        Dim openDlg = New OpenFileDialog()
        openDlg.Filter = "里程列表|*.txt"
        openDlg.InitialDirectory = GlobalShared.defaultpath
        If openDlg.ShowDialog() = DialogResult.OK Then
            Return openDlg.FileName
        Else
            Return Nothing
        End If
    End Function
    ''' <summary>
    ''' 打开工程文件
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function openPro() As String
        Dim openDlg = New OpenFileDialog()
        openDlg.Filter = "工程文件|*.pro"
        openDlg.InitialDirectory = GlobalShared.defaultpath
        If openDlg.ShowDialog() = DialogResult.OK Then
            Return openDlg.FileName
        Else
            Return Nothing
        End If

    End Function
    ''' <summary>
    ''' 获取当前计算机中CAD列表
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetCADList() As List(Of String)
        Dim key As Global.Microsoft.Win32.RegistryKey = Global.Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\Autodesk\AutoCAD")  'AutoCAD软件安装注册表路径
        Dim subKeyNames As String() = key.GetSubKeyNames  '获取所有版本列表名称（所有子项名称）
        'Dim subKeyNamess As String() = key.GetSubKeyNames '获取所有版本列表名称（所有子项名称）
        '如果只检测是否已经安装CAD，只要判断subKeyNames 元素个数就可以了(>0)  没有安装(=0)
        MessageBox.Show("已安装AutoCAD版本数量 " & subKeyNames.Length)
        Dim cadlist As New List(Of String)
        cadlist.Clear()
        '以下是输出CAD版本相应信息
        Dim str As String = ""
        For Each str In subKeyNames
            MessageBox.Show("版本号 " & str) '版本号 如：AutoCAD 2009 返回 R17.2
            Dim key2 As Global.Microsoft.Win32.RegistryKey = key.OpenSubKey(str)  '以只读方式检索子项
            Dim strArray2 As String() = key2.GetSubKeyNames '获取子项数组
            Dim str2 As String = ""
            For Each str2 In strArray2
                Dim key3 As Global.Microsoft.Win32.RegistryKey = key2.OpenSubKey(str2)
                Dim str3 As String = key3.GetValue("ProductName").ToString '版本名称
                MessageBox.Show("版本名称 " & str3) '版本名称 如：str=R17.2 返回 AutoCAD 2009
                cadlist.Add(str3)
            Next
        Next
        Return cadlist
    End Function
    ''' <summary>
    ''' 将点云输出到文件
    ''' </summary>
    ''' <param name="ptslist"></param>
    ''' <param name="mil"></param>
    ''' <param name="pathfile"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function outputpts(ByVal ptslist As List(Of Point3d), ByVal mil As Double, ByVal pathfile As String)
        Dim pubpath = pathfile + "\result\" + mil.ToString() + ".txt"
        Dim stream As New FileStream(pubpath, FileMode.Create)
        Dim writer As New StreamWriter(stream, System.Text.Encoding.UTF8)
        Dim str As String
        For i = 0 To ptslist.Count - 1
            str = i.ToString() + ",," + Math.Round(ptslist(i).p_x, 3).ToString() + "," + Math.Round(ptslist(i).p_y, 3).ToString() + "," + "0"
            writer.WriteLine(str)
            writer.Flush()
        Next
        writer.Close()
        stream.Close()
        '修改文件扩展名
        Dim Myfile = IO.Path.ChangeExtension(pubpath, ".dat")
        Microsoft.VisualBasic.FileSystem.Rename(pubpath, Myfile)
        Return Nothing
    End Function

    ''' <summary>
    ''' 将点云输出到文件
    ''' </summary>
    ''' <param name="pathfile"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function outputpts(ByVal resultlist As List(Of CircleResult), ByVal pathfile As String, ByRef timenow As String, ByRef pro As ProgressBar)
        Dim pubpath As String
        Dim allpts4true As String
        allpts4true = pathfile + "\result\" + timenow + "\allsectionpt.txt"
        For i = 0 To resultlist.Count - 1

            pro.Value = Math.Round((i + 1) * 100 / resultlist.Count)

            resultlist(i).standmil()
            pubpath = pathfile + "\result\" + timenow + "\断面文件\" + resultlist(i).stand_mil + "断面点.dat"

            Directory.CreateDirectory(pathfile + "\result\" + timenow + "\断面文件")
            Directory.CreateDirectory(pathfile + "\result\" + timenow + "\CAD文件")
            Directory.CreateDirectory(pathfile + "\result\" + timenow + "\EXCEL文件")
            ''写假坐标进文件
            Dim writer As New StreamWriter(pubpath, False, System.Text.Encoding.UTF8)
            Dim str As String
            For j = 0 To resultlist(i).false_ptlist.Count - 1
                str = j.ToString() + ",," + Math.Round(resultlist(i).false_ptlist(j).p_x, 3).ToString() + "," + Math.Round(resultlist(i).false_ptlist(j).p_y, 3).ToString() + "," + "0"
                writer.WriteLine(str)
                writer.Flush()
            Next
            writer.Close()
            writer = Nothing

            ''写真坐标进文件
            Dim allwriter As New StreamWriter(allpts4true, True, System.Text.Encoding.UTF8)
            Dim allstr As String
            For j = 0 To resultlist(i).true_ptlist.Count - 1
                allstr = j.ToString() + "," + Math.Round(resultlist(i).true_ptlist(j).p_x, 3).ToString() + "," + Math.Round(resultlist(i).true_ptlist(j).p_y, 3).ToString() + "," + Math.Round(resultlist(i).true_ptlist(j).p_z, 3).ToString()
                allwriter.WriteLine(allstr)
                allwriter.Flush()
            Next
            allwriter.Close()
            allwriter = Nothing

            '修改文件扩展名
            'Dim Myfile = IO.Path.ChangeExtension(pubpath, ".dat")
            'Microsoft.VisualBasic.FileSystem.Rename(pubpath, Myfile)
            '写检测结果文件
            pubpath = pathfile + "\result\" + timenow + "\断面文件\" + resultlist(i).stand_mil + "检测结果.txt"
            Dim writer2 As New StreamWriter(pubpath, False)
            Dim str2 As String
            str2 = "断面里程为：" + resultlist(i).stand_mil
            writer2.WriteLine(str2)
            str2 = String.Format("隧道设计中心坐标为：{0},{1}", Math.Round(resultlist(i).design_center.p_x, 3), Math.Round(resultlist(i).design_center.p_y, 3))
            writer2.WriteLine(str2)
            str2 = String.Format("隧道拟合中心坐标为：{0},{1}", Math.Round(resultlist(i).suvery_center.p_x, 3), Math.Round(resultlist(i).suvery_center.p_y, 3))
            writer2.WriteLine(str2)
            str2 = "拟合半径为：" + Math.Round(resultlist(i).fitcircle.radius, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "设计轨面高程为：" + Math.Round(resultlist(i).stand_ele, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "隧道顶高程为：" + Math.Round(resultlist(i).max_ele, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "隧道底高程为：" + Math.Round(resultlist(i).min_ele, 3).ToString()
            writer2.WriteLine(str2)
            str2 = ""
            writer2.Close()
            writer2 = Nothing
        Next

        Return Nothing
    End Function

    ''' <summary>
    ''' 将点云输出到文件
    ''' </summary>
    ''' <param name="resultlist">马蹄形断面点文件</param>
    ''' <param name="pathfile"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function outputpts(ByVal resultlist As List(Of HorseResult), ByVal pathfile As String, ByRef timenow As String, ByRef pro As ProgressBar)
        Dim pubpath As String
        Dim allpts4true As String
        allpts4true = pathfile + "\result\" + timenow + "\allsectionpt.txt"
        For i = 0 To resultlist.Count - 1

            pro.Value = Math.Round((i + 1) * 100 / resultlist.Count)

            resultlist(i).standmil()
            pubpath = pathfile + "\result\" + timenow + "\断面文件\" + resultlist(i).stand_mil + "断面点.dat"
            Directory.CreateDirectory(pathfile + "\result\" + timenow + "\断面文件")
            Directory.CreateDirectory(pathfile + "\result\" + timenow + "\CAD文件")
            Directory.CreateDirectory(pathfile + "\result\" + timenow + "\EXCEL文件")
            Dim writer As New StreamWriter(pubpath, False, System.Text.Encoding.UTF8)
            Dim str As String
            For j = 0 To resultlist(i).false_ptlist.Count - 1
                str = j.ToString() + ",," + Math.Round(resultlist(i).false_ptlist(j).p_x, 3).ToString() + "," + Math.Round(resultlist(i).false_ptlist(j).p_y, 3).ToString() + "," + "0"
                writer.WriteLine(str)
                writer.Flush()
            Next
            writer.Close()
            writer = Nothing

            ''写真坐标进文件
            Dim allwriter As New StreamWriter(allpts4true, True, System.Text.Encoding.UTF8)
            Dim allstr As String
            For j = 0 To resultlist(i).true_ptlist.Count - 1
                allstr = j.ToString() + "," + Math.Round(resultlist(i).true_ptlist(j).p_x, 3).ToString() + "," + Math.Round(resultlist(i).true_ptlist(j).p_y, 3).ToString() + "," + Math.Round(resultlist(i).true_ptlist(j).p_z, 3).ToString()
                allwriter.WriteLine(allstr)
                allwriter.Flush()
            Next
            allwriter.Close()
            allwriter = Nothing

            '修改文件扩展名
            'Dim Myfile = IO.Path.ChangeExtension(pubpath, ".dat")
            'Microsoft.VisualBasic.FileSystem.Rename(pubpath, Myfile)
            '写检测结果文件
            pubpath = pathfile + "\result\" + timenow + "\断面文件\" + resultlist(i).stand_mil + "检测结果.txt"
            Dim writer2 As New StreamWriter(pubpath, False)
            Dim str2 As String
            str2 = "断面里程为：" + resultlist(i).stand_mil
            writer2.WriteLine(str2)
            str2 = "隧道设计中心坐标为：" + Math.Round(resultlist(i).design_center.p_x, 3).ToString() + "," + Math.Round(resultlist(i).design_center.p_y, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "隧道拟合中心坐标为：" + Math.Round(resultlist(i).suvery_center.p_x, 3).ToString() + "," + Math.Round(resultlist(i).suvery_center.p_y, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "顶圆拟合半径为：" + Math.Round(resultlist(i).fitcircle_up.radius, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "底圆拟合半径为：" + Math.Round(resultlist(i).fitcircle_down.radius, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "设计轨面高程为：" + Math.Round(resultlist(i).stand_ele, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "隧道顶高程为：" + Math.Round(resultlist(i).max_ele, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "隧道底高程为：" + Math.Round(resultlist(i).min_ele, 3).ToString()
            writer2.WriteLine(str2)
            str2 = ""
            writer2.Close()
            writer2 = Nothing
        Next
        Return Nothing
    End Function

    ''' <summary>
    ''' 将点云输出到文件
    ''' </summary>
    ''' <param name="resultlist">方形断面点文件</param>
    ''' <param name="pathfile"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function outputpts(ByVal resultlist As List(Of RectangleResult), ByVal pathfile As String, ByRef timenow As String, ByRef pro As ProgressBar)
        Dim pubpath As String
        Dim allpts4true As String
        allpts4true = pathfile + "\result\" + timenow + "\allsectionpt.txt"
        For i = 0 To resultlist.Count - 1

            pro.Value = Math.Round((i + 1) * 100 / resultlist.Count)

            resultlist(i).standmil()
            pubpath = pathfile + "\result\" + timenow + "\断面文件\" + resultlist(i).stand_mil + "断面点.dat"
            Directory.CreateDirectory(pathfile + "\result\" + timenow + "\断面文件")
            Directory.CreateDirectory(pathfile + "\result\" + timenow + "\CAD文件")
            Directory.CreateDirectory(pathfile + "\result\" + timenow + "\EXCEL文件")
            Dim writer As New StreamWriter(pubpath, False, System.Text.Encoding.UTF8)
            Dim str As String
            For j = 0 To resultlist(i).false_ptlist.Count - 1
                str = j.ToString() + ",," + Math.Round(resultlist(i).false_ptlist(j).p_x, 3).ToString() + "," + Math.Round(resultlist(i).false_ptlist(j).p_y, 3).ToString() + "," + "0"
                writer.WriteLine(str)
                writer.Flush()
            Next
            writer.Close()
            writer = Nothing

            ''写真坐标进文件
            Dim allwriter As New StreamWriter(allpts4true, True, System.Text.Encoding.UTF8)
            Dim allstr As String
            For j = 0 To resultlist(i).true_ptlist.Count - 1
                allstr = j.ToString() + "," + Math.Round(resultlist(i).true_ptlist(j).p_x, 3).ToString() + "," + Math.Round(resultlist(i).true_ptlist(j).p_y, 3).ToString() + "," + Math.Round(resultlist(i).true_ptlist(j).p_z, 3).ToString()
                allwriter.WriteLine(allstr)
                allwriter.Flush()
            Next
            allwriter.Close()
            allwriter = Nothing

            '修改文件扩展名
            'Dim Myfile = IO.Path.ChangeExtension(pubpath, ".dat")
            'Microsoft.VisualBasic.FileSystem.Rename(pubpath, Myfile)
            '写检测结果文件
            pubpath = pathfile + "\result\" + timenow + "\断面文件\" + resultlist(i).stand_mil + "检测结果.txt"
            Dim writer2 As New StreamWriter(pubpath, False)
            Dim str2 As String
            str2 = "断面里程为：" + resultlist(i).stand_mil
            writer2.WriteLine(str2)
            str2 = "隧道设计中心坐标为：" + Math.Round(resultlist(i).design_center.p_x, 3).ToString() + "," + Math.Round(resultlist(i).design_center.p_y, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "轨面处中心距侧墙距离为：" + Math.Round(resultlist(i).difCeGG, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "轨面上2100mm处中心距侧墙距离为：" + Math.Round(resultlist(i).difCeGG_21, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "轨面上4200mm处中心距侧墙距离为：" + Math.Round(resultlist(i).difCeGG_42, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "轨面距车站顶部距离：" + Math.Round(resultlist(i).difmax_ele, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "轨面距车站底部距离：" + Math.Round(resultlist(i).difmin_ele, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "中心线至站台边缘的距离：" + Math.Round(resultlist(i).dif_zth, 3).ToString()
            writer2.WriteLine(str2)
            str2 = "站台边缘顶距轨面高度：" + Math.Round(resultlist(i).dif_ztv, 3).ToString()
            writer2.WriteLine(str2)
            str2 = ""
            writer2.Close()
            writer2 = Nothing
        Next
        Return Nothing
    End Function
End Module